<template>
  <div class="changePsdForm">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="原密码" prop="password">
        <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="ruleForm.newPassword" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPsd">
        <el-input v-model="ruleForm.checkPsd" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">确认修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import User from '@/api/user'
import Message from '@/utils/message'
import Cookie from 'js-cookie'
import  jwt  from  'jsonwebtoken'

export default {
  name: 'ChangePsd',
  data() {
    var newPsd = (rule, value, callback) => {
      if (value.trim() === '') {
        return callback(new Error('密码不能为纯空格'))
      }
      var reg = /[a-z\d#~._!@$%^&*+]/i
      if (!reg.test(value)) {
        return callback(new Error('密码由数字或字母或特殊字符组成（~._!@#$%^&*+”）'))
      }
      if (
        value.trim() &&
        value.trim().length >= 6 &&
        value.trim().length <= 16
      ) {
        callback()
      } else {
        return callback(new Error('密码长度为6到16个字符'))
      }
    }
    var checkPsd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        password: '',
        newPassword: '',
        checkPsd: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: newPsd, trigger: 'blur' }
        ],
        checkPsd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: checkPsd, trigger: 'blur' },
          { validator: newPsd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let  jwt = require('jsonwebtoken');
          if(location.port===''){
            var token =   jwt.decode(Cookie.get('access_token_marketing'))
          }else{
            var token =   jwt.decode(Cookie.get('access_token_9528'))
          }
          const objData = {
            oldPassword: this.ruleForm.password,
            newPassword: this.ruleForm.newPassword,
            userId: token.user_info.user_id
          }
          const result = await User.changePassword(objData)
          if (result) {
            Message({
              type: 'success',
              message: '修改成功!'
            })
            this.$store.dispatch('user/fedLogOut')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.changePsdForm {
  padding: 60px 30px;
}
.el-input {
  width: 300px;
}
</style>
